(function() {
	var tabObj =  null; // table控件

	window.nameLink = function (cellData, dom, data) {
		var id = dom.children().eq(0).html(),
			url = keter.reqRoot+'/view/'+id;
		return '<a href="'+url+'">' + cellData + '</a>';
	};
	
	window.viewComments = function (cellData, dom) {
		var id = dom.children().eq(0).html(),
		    status = dom.children().eq(3).html();
		var result = makeResultByStatus(status);
		url = keter.reqRoot+'/viewcomments/'+id;
		return '<a href="'+url+'">' + result + '</a>';
	};
	
	var get = function () {
		$.ajax({
			url: keter.reqRoot + '/getAll',
			context: document.body
		}).done(function(data) {
			tabObj = $('.kt_table').createTable({
				data : data,
				rowFn : function (data) {
				},
				updateFn : function (data) {
					//FIXME: 请求中带上菜单参数
					window.location.href = keter.reqRoot + '/update/'+data[0];
				},
				deleteFn : function (data) {
					if(role.indexOf('STUDENT')>=0){
						alert("不允许删除课题！");
						return false;
					}
					if(window.confirm('确定删除该记录？')){
						 $.ajax({
								url: keter.reqRoot + '/delete/'+data[0],
								contentType:'application/json',
								context: document.body
							}).done(function(data){
								success(data,true);
							});
						return true;
					} else {
						return false;
					}
				}
			});
		});
	};

	var dataContrl = function () {
		// 创建功能
		$('#addBtn').click(function () {
			if(role.indexOf('STUDENT')>=0){
				//TODO: 判断roleNames是否为学生，防止学生重复创建课题
			}
			window.location.href = keter.reqRoot+'/add/';
		});
		
		$('#submitBtn').click(function () {
			if (window.confirm('确认将当前课题提交给您的导师？')) {
				var $node = tabObj.getRowDataByIndex(0),
					id = $node.id;
				$.postJSON(keter.reqRoot + '/submit/' + id, {}, function (data) {
					success(data, true);
				});
				return true;
			} else {
				return false;
			}
		});
	};

	var main = function () {
		get();
		dataContrl();
	};

	return main();
})();
